package com.edie.customview.custom;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChild2;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * edie create on 2018/7/26
 */
public class MyChildView extends LinearLayout implements NestedScrollingChild {

    private NestedScrollingChildHelper mNestedScrollingChildHelper;
    private final int[] offset = new int[2]; //偏移量
    private final int[] consumed = new int[2]; //消费
    private int lastY;
    private int showHeight;


    public MyChildView(Context context) {
        super(context);
    }

    public MyChildView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyChildView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyChildView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            //按下
            case MotionEvent.ACTION_DOWN:
                lastY = (int) event.getRawY();
                break;
            //移动
            case MotionEvent.ACTION_MOVE:
                int y = (int) (event.getRawY());
                int dy = y - lastY;
                lastY = y;
                if (startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL)
                        && dispatchNestedPreScroll(0, dy, consumed, offset)) //如果找到了支持嵌套滑动的父类,父类进行了一系列的滑动
                {
                    //获取滑动距离
                    int remain = dy - consumed[1];
                    if (remain != 0) {
                        scrollBy(0, -remain);
                    }

                } else {
                    scrollBy(0, -dy);
                }
                break;
        }

        return true;
//        return super.onTouchEvent(event);
    }

//    @Override
//    public void scrollTo(int x, int y) {
//        int maxY = getMeasuredHeight() - showHeight;
//        if (y > maxY) {
//            y = maxY;
//        }
//        if (y < 0) {
//            y = 0;
//        }
//        super.scrollTo(x, y);
//    }
//    //初始化helper对象
//    private NestedScrollingChildHelper getScrollingChildHelper() {
//        if (mNestedScrollingChildHelper == null) {
//            mNestedScrollingChildHelper = new NestedScrollingChildHelper(this);
//            mNestedScrollingChildHelper.setNestedScrollingEnabled(true);
//        }
//        return mNestedScrollingChildHelper;
//    }

    //实现一下接口
//    @Override
//    public void setNestedScrollingEnabled(boolean enabled) {
//        getScrollingChildHelper().setNestedScrollingEnabled(enabled);
//    }
//
//    @Override
//    public boolean isNestedScrollingEnabled() {
//        return getScrollingChildHelper().isNestedScrollingEnabled();
//    }
//
//    @Override
//    public boolean startNestedScroll(int axes) {
//        return getScrollingChildHelper().startNestedScroll(axes);
//    }
//
//    @Override
//    public void stopNestedScroll() {
//        getScrollingChildHelper().stopNestedScroll();
//    }
//
//    @Override
//    public boolean hasNestedScrollingParent() {
//        return getScrollingChildHelper().hasNestedScrollingParent();
//    }
//
//    @Override
//    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
//        return getScrollingChildHelper().dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
//    }
//
//    @Override
//    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
//        return getScrollingChildHelper().dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
//    }
//
//    @Override
//    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
//        return getScrollingChildHelper().dispatchNestedFling(velocityX, velocityY, consumed);
//    }
//
//    @Override
//    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
//        return getScrollingChildHelper().dispatchNestedPreFling(velocityX, velocityY);
//    }

//----
//    @Override
//    public boolean startNestedScroll(int axes, int type) {
//        return false;
//    }
//
//    @Override
//    public void stopNestedScroll(int type) {
//
//    }
//
//    @Override
//    public boolean hasNestedScrollingParent(int type) {
//        return false;
//    }
//
//    @Override
//    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow, int type) {
//        return false;
//    }
//
//    @Override
//    public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow, int type) {
//        return false;
//    }
}
